<%@page import="classes.Valuta"%>
<%@page import="models.User"%>
<%@include file="template/configuration.jsp" %>
<%@page import="models.Category"%>
<%@page import="java.util.List"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>${website} || ${uq_manage_categories_and_courses}</title>
        ${stylesheet}
        ${jquery}
        ${jstooltip}
        ${jscollapse}
        ${jspagination}
    </head>
    <body>
        <div class="container">
            <%@include file="template/header.jsp" %>
            <div class="content">
                <h1>${uq_manage_categories_and_courses}</h1>
                <c:if test="${sessionScope.userrole == 'Administrator'}">
                    <div class="third-block no-height">
                        <a href="ManageCategory">${uq_add_a_new_category} <img src="${url}images/add.png" border="0" class="icon-text"/></a>
                    </div>
                </c:if>
                <!-- clear //-->
                <div class="clear margin-bottom-10"></div>
                <c:set var="first" value="true" />
                <c:choose>
                    <c:when test="${fn:length(categories)!=0}">                        
                        <c:forEach items="${categories}" var="category">
                                    <c:set var="catid" value="${category.id}" /> 
                                    <%
                                    // hu(mc), cd(mc), mc = manage categories
                                    HibernateUtil humc = new HibernateUtil();
                                    CourseDao cdmc = new CourseDao(humc.openSession());
                                    HttpSession sessie = request.getSession();
                                    List<Course> courseListMC = null;
                                    if (sessie.getAttribute("userrole").equals("Administrator")) {
                                        courseListMC = cdmc.findAllByCategory(Long.parseLong(pageContext.getAttribute("catid").toString()), 15);
                                    } else {
                                        User user = (User) sessie.getAttribute("user");
                                        courseListMC = cdmc.findAllByTeacherAndCategory(user.getMember_id(), Long.parseLong(pageContext.getAttribute("catid").toString()));
                                    }
                                    request.setAttribute("courses", courseListMC);           
                                    if (courseListMC.size() > 0 || sessie.getAttribute("userrole").equals("Administrator")) {                                         
                                    %>                                                                
                            

                            <div class="bundle" <c:if test="${first != 'true'}">style="margin-top: -50px;"</c:if>>
                                <c:set var="first" value="false" />
                                <div class="top">
                                    <div class="left">
                                        <c:if test="${sessionScope.userrole == 'Administrator'}">
                                            <a href="${url}Category?id=${category.id}"><img src="${url}images/edit.png" /></a> 
                                            <c:choose><c:when test="${fn:length(category.cat)>0}"><img src="${url}images/remove-disabled.png"/></c:when><c:otherwise> <a href="${url}Category?idremove=${category.id}" class="remove"><img src="${url}images/remove.png" /></a></c:otherwise> </c:choose>
                                            <span class="info">${uq_category} <strong>${category.name}</strong></span>
                                        </c:if>
                                    </div>
                                    <div class="center">
                                        <h5>${category.name}</h5>
                                    </div>
                                    <div class="right">
                                        <c:if test="${sessionScope.userrole == 'Administrator'}">
                                            <a href="ManageCourse/form?catid=${category.id}">${uq_add_a_new_course}<img src="${url}images/add.png" /></a>
                                        </c:if>
                                    </div>
                                </div>
                                <!-- clear //-->
                                <div class="clear"></div>
                                <div class="full-block" style="min-height: 26px;"> 
                                    <% if (courseListMC.size() > 0){%>
                                        <div style="display: none;">
                                            <table class="overview">
                                                <tr>
                                                    <th>${uq_course}</th>
                                                    <th>${uq_description}</th>
                                                    <th>${uq_price}</th>
                                                    <th>${uq_teachers}</th>
                                                    <th class="date">${uq_added}</th>
                                                    <th class="manage">${uq_manage}</th>
                                                </tr>
                                                <c:set var="i" value="0"/>
                                                <c:forEach items="${courses}" var="course">
                                                    <c:set var="i" value="${i + 1}"/>
                                                    <tr class="<c:choose><c:when test="${i % 2 == 0}">even</c:when><c:otherwise>odd</c:otherwise></c:choose>">
                                                        <td>${course.name}</td>
                                                        <td>
                                                            <c:choose>
                                                                <c:when test="${fn:length(course.description) > 40}">
                                                                     <c:out value="${fn:substring(course.description, 0, 40)}"/>...
                                                                </c:when>
                                                                <c:otherwise>
                                                                    ${course.description}
                                                                </c:otherwise>
                                                            </c:choose>
                                                        </td>
                                                        <td>
                                                            <c:set var="price" value="${course.price}" />
                                                            <%=Valuta.getSignFromCurrency(pageContext.getAttribute("currency").toString())%> 
                                                            <%=Valuta.calculateToCurrency(pageContext.getAttribute("currency").toString(), (Double) Double.parseDouble(pageContext.getAttribute("price").toString()))%>
                                                        </td>
                                                        <td>
                                                            <img src="${url}images/question.png" class="question" />
                                                            <div class="relative">
                                                                <div class="tooltip">
                                                                           <c:forEach items="${course.teacher}" var="teacher">
                                                                                ${teacher.firstname}
                                                                                ${teacher.insertion}
                                                                                ${teacher.lastname}<br/>
                                                                            </c:forEach>
                                                                </div>
                                                            </div>
                                                        </td>
                                                        <td>${course.date}</td>
                                                        <td>
                                                            <a href="ManageCourse/form?idedit=${course.id}"><img src="${url}images/edit.png" /></a> 
                                                            <c:if test="${sessionScope.userrole == 'Administrator'}">
                                                                <a href="ManageCourse/form?idremove=${course.id}" class="remove"><img src="${url}images/remove.png" /></a>
                                                                <span class="info">course <strong>${course.name}</strong></span>
                                                            </c:if>
                                                        </td>
                                                    </tr>
                                                </c:forEach>
                                            </table>
                                            <%
                                            double total_items = cdmc.getLastPage(Long.parseLong(pageContext.getAttribute("catid").toString()));
                                            pageContext.setAttribute("pagination", (int) Math.ceil(total_items / 15));
                                            %>
                                            <c:if test="${pagination > 1}">
                                                <div class="pagination">
                                                    <c:forEach var="i" begin="1" end="${pagination}" step="1">
                                                        <a href="${url}jsp/pagination/manage_categories.jsp?page=${i}&category_id=${category.id}" class="number<c:if test="${i == '1'}"> active</c:if>">${i}</a>
                                                    </c:forEach>
                                                </div>
                                            </c:if>
                                        </div>
                                    <% } else { %>
                                        <div class="no-yet-message" style="display: none;">
                                            ${uq_there_are_no_courses_in_this_category_yet}
                                        </div>
                                    <% } %>
                                </div>
                                <!-- clear //-->
                                <div class="clear margin-bottom-10" style="display: none;"></div>
                            </div>
                                <% } %>
                        </c:forEach>
                    </c:when>
                    <c:otherwise>
                        <div class="full-block">
                            ${uq_there_are_no_categories_added_yet}
                        </div>
                    </c:otherwise>
                </c:choose>
            </div>
            <%@include file="template/footer.jsp" %>
        </div>
    </body>
</html>
